package com.it2297.view;

import javax.swing.JPanel;

import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;
import javax.swing.DefaultComboBoxModel;

import com.it2297.controller.WorkshopController;
import com.it2297.model.Trainer;
import com.it2297.model.Workshop;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SpinnerDateModel;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import javax.swing.JTextPane;

public class UpdateWorkshopView extends JPanel {
	private final WorkshopController control=new WorkshopController(this);
	private final JTextField workshopidtxt;
	private final JTextField locationtxt;
	private final  JTextField titletxt;
	private final JSpinner capacity=new JSpinner();
	private final  JSpinner slot=new JSpinner();
	private final  JComboBox statusbox=new JComboBox();
	private final  JComboBox trainerBox=new JComboBox();
	private final JTextField dstxt;
	private final JTextField detxt;
	private final JTextField descriptionarea;
	
	/**
	 * Create the panel.
	 */
	public UpdateWorkshopView() {
		setLayout(null);
		
		JLabel lblUpdateWorkshop = new JLabel("Update Workshop");
		lblUpdateWorkshop.setFont(new Font("Tahoma", Font.ITALIC, 19));
		lblUpdateWorkshop.setBounds(25, 11, 244, 35);
		add(lblUpdateWorkshop);
		
		JLabel lblWorkshopId = new JLabel("Workshop ID:");
		lblWorkshopId.setBounds(25, 73, 118, 14);
		add(lblWorkshopId);
		
		workshopidtxt = new JTextField();
		workshopidtxt.setBounds(182, 70, 321, 20);
		add(workshopidtxt);
		workshopidtxt.setColumns(10);
		
		JLabel lblTitle = new JLabel("Title:");
		lblTitle.setBounds(25, 117, 73, 14);
		add(lblTitle);
		
		JLabel lblDateStart = new JLabel("Date Start:\r\n");
		lblDateStart.setBounds(25, 155, 83, 14);
		add(lblDateStart);
		
		JLabel lblDateEnd = new JLabel("Date End:");
		lblDateEnd.setBounds(25, 191, 83, 14);
		add(lblDateEnd);
		
		JLabel lbldescription = new JLabel("Description:\r\n\r\n");
		lbldescription.setBounds(25, 235, 73, 14);
		add(lbldescription);
		
		JLabel lbllocation = new JLabel("Location:");
		lbllocation.setBounds(25, 292, 83, 14);
		add(lbllocation);
		
		JLabel lblCapacity = new JLabel("Capacity:");
		lblCapacity.setBounds(25, 331, 73, 14);
		add(lblCapacity);
		
		JLabel lblAvailableSlot = new JLabel("Available Slot:");
		lblAvailableSlot.setBounds(25, 372, 83, 14);
		add(lblAvailableSlot);
		
		JLabel lblStatus = new JLabel("Status:");
		lblStatus.setBounds(25, 419, 83, 14);
		add(lblStatus);
		
		JButton btnConfirm = new JButton("Confirm");
		btnConfirm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				UpdateWorkshop();
				JOptionPane.showMessageDialog(null, "You Have Successfully Updated The Workshop");
			}
		});
		btnConfirm.setBounds(63, 512, 118, 23);
		add(btnConfirm);
		
		JButton btnRetrieve = new JButton("Retrieve");
		btnRetrieve.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(workshopidtxt.getText().isEmpty()){
					JOptionPane.showMessageDialog(null,"Please fill in Workshop Id field of the workshop that you would like to update");
				}
				else{
			    int workshopid=Integer.parseInt(workshopidtxt.getText());
				control.getWorkshop(workshopid);
				workshopidtxt.setEditable(false);
				
				if(titletxt.getText().isEmpty()){
					JOptionPane.showMessageDialog(null,"Workshop cannot be found");
					workshopidtxt.setEditable(true);
				}
				else{
					JOptionPane.showMessageDialog(null,"Workshop found");
				}
				}
				
			}
		});
		btnRetrieve.setBounds(207, 512, 118, 23);
		add(btnRetrieve);
		
		
		statusbox.setModel(new DefaultComboBoxModel(new String[] {"Ava", "Nva"}));
		statusbox.setBounds(182, 416, 164, 20);
		add(statusbox);
		
		
		slot.setModel(new SpinnerNumberModel(0, null, 300, 1));
		slot.setBounds(182, 369, 203, 20);
		add(slot);
		
		
		capacity.setModel(new SpinnerNumberModel(10, 10, 300, 1));
		capacity.setBounds(182, 328, 203, 20);
		add(capacity);
		
		locationtxt = new JTextField();
		locationtxt.setBounds(182, 289, 321, 20);
		add(locationtxt);
		locationtxt.setColumns(10);
		
		titletxt = new JTextField();
		titletxt.setBounds(182, 114, 321, 20);
		add(titletxt);
		titletxt.setColumns(10);
		
		JLabel lblTrainerNric = new JLabel("Trainer NRIC");
		lblTrainerNric.setBounds(25, 466, 97, 14);
		add(lblTrainerNric);
		
		
		trainerBox.setBounds(182, 463, 164, 20);
		for(int i=0; i< Trainer.getMultiple().size();i++){
			trainerBox.addItem(Trainer.getMultiple().get(i).getNric());
			}
		add(trainerBox);
		
		dstxt = new JTextField();
		dstxt.setBounds(182, 152, 321, 20);
		add(dstxt);
		dstxt.setColumns(10);
		
		detxt = new JTextField();
		detxt.setBounds(182, 188, 321, 20);
		add(detxt);
		detxt.setColumns(10);
		
		descriptionarea = new JTextField();
		descriptionarea.setBounds(183, 232, 320, 46);
		add(descriptionarea);
		descriptionarea.setColumns(10);
		
		JButton btnRefresh = new JButton("Refresh ");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				workshopidtxt.setEditable(true);
				workshopidtxt.setText("");
				titletxt.setText("");
				locationtxt.setText("");
				descriptionarea.setText("");
				dstxt.setText("");
				detxt.setText("");
				capacity.setValue(0);
				slot.setValue(0);
				statusbox.removeAllItems();
				
			}
		});
		btnRefresh.setBounds(353, 512, 110, 23);
		add(btnRefresh);

	}
	public String getStatus() {
		return statusbox.getSelectedItem().toString();
	}
	public void setStatus(String status){
		statusbox.setSelectedItem(status);
	}

	public int getWorkshopId() {
		return Integer.parseInt(workshopidtxt.getText());
	}
		
	
	public String getTitle(){
		return titletxt.getText();
	}
	public void setTitle(String title){
		titletxt.setText(title);
	}
	public String getlocation() {
		return locationtxt.getText();
	}
	public void setlocation(String location){
		locationtxt.setText(location);
	}
	public int getCapacity(){
		return (int) capacity.getValue();
	}
	public void setCapacity(int totalcapacity){
		capacity.setValue(totalcapacity);
	}
	public int getAvailableSlot(){
		return (int) slot.getValue();
	}
	public void setAvailableSlot(int availableslot){
		slot.setValue(availableslot);
	}
	
	public String getDescription(){
		return descriptionarea.getText();
	}
	public void setDescription(String description){
		descriptionarea.setText(description);
	}
	public void setStartDate(Date dateStart){
		 if(!(dateStart == null)){
		 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		 String reportDate = df.format(dateStart);
		 dstxt.setText(reportDate);
		 }
	}
	
	
	public Date getStartDate(){
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
		Date date = null;
		try {
		  date=	(Date)formatter.parse(dstxt.getText());
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		System.out.println(date);
		 return date;
		
		
		
		
	}
		public Date getEndDate(){
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
			Date date=null;
			try {
				date = (Date)formatter.parse(detxt.getText());
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			System.out.println(date);
			 return date;
		
		}
		public void setEndDate(Date dateEnd){
			 if(!(dateEnd == null)){
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			 String reportDate = df.format(dateEnd);
			 detxt.setText(reportDate);
			 }
		}
		public String getNRIC(){
			return (String) trainerBox.getSelectedItem();
		}
		public void setNRIC(String trainernric){
			trainerBox.setSelectedItem(trainernric);
		}
		
		
	
	private void UpdateWorkshop(){
		control.UpdateWorkshop();
	}
}
